#include "admini_demo.h"
static lv_obj_t* cont;
static lv_obj_t* hisbox;
static lv_obj_t* receiptbox;
static FILE *fp1;
static FILE *fp2;
static char *buf1;
static char *buf2;
static void lv_receipt_box_create(void);
static void lv_history_box_create(void);
static void admin_btn_event_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    BtnFunc func_id = (BtnFunc)(uintptr_t)lv_event_get_user_data(e);  // 取出用户数据

    switch(func_id) {
        case BTN_ADD: printf("点击了增加菜品\n");  lv_obj_del(cont) ;add_tea_jm();  break;
        case BTN_DEL: printf("点击了删除菜品\n"); lv_obj_del(cont) ;del_tea_jm();break;
        case BTN_MODIFY: printf("点击了修改菜品\n"); lv_obj_del(cont) ;chmod_tea_jm();break;
        case BTN_CHECK: printf("点击了查询库存\n");lv_obj_del(cont) ;tea_main();list_for_each(head);break;
        case BTN_VIEW_SALE: printf("点击了查看流水\n");lv_history_box_create();lv_obj_clear_flag(hisbox,LV_OBJ_FLAG_HIDDEN); break;
        case BTN_PRICE: printf("点击了导出票据\n"); lv_receipt_box_create();lv_obj_clear_flag(receiptbox,LV_OBJ_FLAG_HIDDEN);break;
        case BTN_BACK: printf("点击了返回主界面\n"); lv_obj_del(cont) ;mygui(); break;
        case BTN_EXPORT: printf("暂无\n"); break;
        case BTN_RESTOCK: printf("暂无\n"); break;
    }
}

#if 1 
static void lv_mainstart_2(void)
{
     static lv_coord_t col_dsc[] = {150, 150, 150, LV_GRID_TEMPLATE_LAST};  //列宽度
     static lv_coord_t row_dsc[] = {50, 50, 50, LV_GRID_TEMPLATE_LAST};  //行宽度
     const char *btn_texts[9] = {
        "增加奶茶", "删除奶茶", "修改奶茶信息",
        "查询库存", "查看流水", "导出票据", 
        "返回主界面","暂无", "暂无",
    };
    
    /*Create a container with grid*/
    cont = lv_obj_create(lv_scr_act());
    lv_obj_set_style_grid_column_dsc_array(cont, col_dsc, 0);
    lv_obj_set_style_grid_row_dsc_array(cont, row_dsc, 0);
    lv_obj_set_size(cont, 500, 220);
    lv_obj_center(cont);
    lv_obj_set_layout(cont, LV_LAYOUT_GRID);

    lv_obj_t * label;
    lv_obj_t * obj;

    uint32_t i;
    for(i = 0; i < 9; i++) {
        uint8_t col = i % 3;
        uint8_t row = i / 3;

        obj = lv_btn_create(cont);
        /*Stretch the cell horizontally and vertically too Set span to 1 to make the cell 1 column/row sized*/
        lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_CENTER, col, 1,  LV_GRID_ALIGN_CENTER, row, 1);
        lv_obj_add_event_cb(obj, admin_btn_event_cb, LV_EVENT_CLICKED, (void *)(uintptr_t)i);
        label = lv_label_create(obj);
        
        lv_label_set_text_fmt(label, "%s", btn_texts[i]);
        lv_obj_set_style_text_font(label,&lv_font_source_han_sans_bold_14,0);
        lv_obj_center(label);
    }
}
#endif

//-------------------------------查看流水的弹窗------------------------------------
static void lv_history_box_create(void)
{
    fp1 = fopen("history.txt","rt");
    if (!fp1)
    {
        perror("open history.txt fail");
        return;
    }
    // 获取文件大小
    fseek(fp1, 0, SEEK_END);
    long fsize = ftell(fp1);
    fseek(fp1, 0, SEEK_SET);
    buf1 = malloc(sizeof(char)*1024*1024);
    if (buf1 == NULL)
    {
        perror("分配动态内存失败");
        return;
    }
    
    fread(buf1,fsize+1,1,fp1);

    

    hisbox = lv_msgbox_create(lv_scr_act(),NULL,buf1,NULL,true );
    lv_obj_set_style_text_font(hisbox,&lv_font_source_han_sans_bold_14,0);
    lv_obj_set_size(hisbox,600,440);
    lv_obj_set_style_bg_opa(hisbox,230,0);
    lv_obj_align(hisbox,LV_ALIGN_CENTER,0,0);

    lv_obj_set_style_pad_top(hisbox,15,LV_STATE_DEFAULT);  //顶部填充
    lv_obj_t *close_btnt = lv_msgbox_get_close_btn(hisbox); //获取到关闭图标
    lv_obj_set_style_text_color(close_btnt,lv_color_hex(0x696969),LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(close_btnt,&lv_font_montserrat_20,0);
    lv_obj_set_style_pad_top(close_btnt,0,LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(close_btnt,0,LV_STATE_DEFAULT);
    lv_obj_set_style_border_opa(close_btnt,0,LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_opa(close_btnt,0,LV_STATE_DEFAULT);


    lv_obj_t* msgbox_content = lv_msgbox_get_content(hisbox); //获取到主体
//    lv_obj_set_style_text_color(msgbox_content,lv_color_hex(0xFF0000),LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(msgbox_content,lv_color_hex(0x808080),LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(msgbox_content,10,LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(msgbox_content,5,0);
    lv_obj_set_style_text_line_space(msgbox_content,10,0);

    lv_obj_add_flag(hisbox,LV_OBJ_FLAG_HIDDEN);                 //默认隐藏
    printf("%s\n",buf1);
    free(buf1);
    fclose(fp1);

}

//-------------------------------查看收据的弹窗------------------------------------
static void lv_receipt_box_create(void)
{
    fp2 = fopen("receipt.txt","rt");
    if (!fp2)
    {
        perror("open receipt.txt fail");
        return;
    }
    // 获取文件大小
    fseek(fp2, 0, SEEK_END);
    long fsize = ftell(fp2);
    fseek(fp2, 0, SEEK_SET);
    buf2 = malloc(sizeof(char)*1024*1024);
    if (buf2 == NULL)
    {
        perror("分配动态内存失败");
        return;
    }
     
    fread(buf2,fsize+1,1,fp2);

    receiptbox = lv_msgbox_create(lv_scr_act(),NULL,buf2,NULL,true );
    lv_obj_set_style_text_font(receiptbox,&lv_font_source_han_sans_bold_14,0);
    lv_obj_set_size(receiptbox,400,300);
    lv_obj_set_style_bg_opa(receiptbox,230,0);
    lv_obj_align(receiptbox,LV_ALIGN_CENTER,0,0);

    lv_obj_set_style_pad_top(receiptbox,15,LV_STATE_DEFAULT);  //顶部填充
    lv_obj_t *close_btnt = lv_msgbox_get_close_btn(receiptbox); //获取到关闭图标
    lv_obj_set_style_text_color(close_btnt,lv_color_hex(0x696969),LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(close_btnt,&lv_font_montserrat_20,0);
    lv_obj_set_style_pad_top(close_btnt,0,LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(close_btnt,0,LV_STATE_DEFAULT);
    lv_obj_set_style_border_opa(close_btnt,0,LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_opa(close_btnt,0,LV_STATE_DEFAULT);


    lv_obj_t* msgbox_content = lv_msgbox_get_content(receiptbox); //获取到主体
//    lv_obj_set_style_text_color(msgbox_content,lv_color_hex(0xFF0000),LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(msgbox_content,lv_color_hex(0x808080),LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(msgbox_content,10,LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(msgbox_content,5,0);
    lv_obj_set_style_text_line_space(msgbox_content,10,0);

    lv_obj_add_flag(receiptbox,LV_OBJ_FLAG_HIDDEN);                 //默认隐藏
    printf("%s\n",buf2);
    free(buf2);
    fclose(fp2);

}

void admini_jm(void)
{
    lv_mainstart_2();
}